// pages/login/index.js
import { Login, Register } from '../../api/path/index'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type: 'password',
    checked: false,
    loading: false,
    sts: "登录",
    form: {
      username: '',
      password: ''
    },
    register: {
      username: '',
      password: '',
      password2: ''
    },
    password2Type: 'password',
  },
  forget: function () {
    if (this.data.sts == "注册") {
      this.setData({
        sts: "登录"
      })
    } else {
      this.setData({
        sts: "注册"
      })
    }

  },
  handoverType: function () {
    console.log(1);
    if (this.data.type == 'password') {
      this.setData({
        type: 'text'
      })
    } else {
      this.setData({
        type: 'password'
      })
    }
  },
  handoverType2: function () {
    if (this.data.password2Type == 'password') {
      this.setData({
        password2Type: 'text'
      })
    } else {
      this.setData({
        password2Type: 'password'
      })
    }
  },
  login: function () {
    this.setData({
      loading: true
    })
    Login(this.data.form).then(res => {
      console.log(res);
      if (res.code == 1) {
        setTimeout(() => {
          wx.showToast({
            title: '登录成功',
            icon: 'success',
            duration: 800
          })
          setTimeout(() => {
            wx.setStorageSync('token', JSON.stringify(res.data.token))
            wx.setStorageSync('userinfo', JSON.stringify(res.data.userinfo[0]))
            wx.switchTab({
              url: '/pages/index/index',
            })
            this.setData({
              loading: false
            })
          }, 1000)
        }, 1000)
      } else if (res.code == 0) {
        wx.showToast({
          title: res.msg,
          icon: 'error',
          duration: 800
        })
        setTimeout(() => {
          this.setData({
            loading: false
          })
        }, 1000)
      }
    }).catch(err => {
      this.setData({
        loading: false
      })
    })
  },
  register: function () {
    this.setData({
      loading: true
    })
    if (this.data.register.password == this.data.register.password2) {
      Register(this.data.register).then(res => {
        if (res.code == 1) {
          setTimeout(() => {
            wx.showToast({
              title: '注册成功',
              icon: 'success',
              duration: 800
            })
            this.setData({
              sts: "登录",
              loading: false
            })
          }, 1000)
        } else if (res.code == 1) {
          wx.showToast({
            title: res.msg,
            icon: 'error',
            duration: 800
          })
          this.setData({
            loading: false
          })
        }
      }).catch(err => {
        this.setData({
          loading: false
        })
      })
    } else {
      wx.showToast({
        title: '两次密码不一致',
        icon: 'error',
        duration: 800
      })
    }
  },
  onChange: function (e) {
    this.setData({
      checked: e.detail
    })
  },
  inputChange: function (e) {
    this.setData({
      'form.username': e.detail.value
    });
  },
  inputChange1: function (e) {
    this.setData({
      'form.password': e.detail.value
    });
  },
  inputChange3: function (e) {
    this.setData({
      'register.username': e.detail.value
    });
  },
  inputChange4: function (e) {
    this.setData({
      'register.password': e.detail.value
    });
  },
  inputChange5: function (e) {
    this.setData({
      'register.password2': e.detail.value
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})